package ocean.chinacraft.common.world.block.decoration;

import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.fluid.FluidState;
import net.minecraft.fluid.Fluids;
import net.minecraft.item.BlockItemUseContext;
import net.minecraft.tags.BlockTags;
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockReader;
import ocean.chinacraft.common.world.block.decoration.base.AbstractWindow;
import ocean.chinacraft.common.world.block.properties.PropertiesMisc;

/**
 * 窗格
 *
 * @author zhang
 * @since 2021/12/10
 */
public class Window extends AbstractWindow {

    public Window() {
        super(PropertiesMisc.defaultWindow());
    }

    @Override
    public BlockState getStateForPlacement(BlockItemUseContext context) {
        IBlockReader iblockreader = context.getLevel();
        BlockPos blockpos = context.getClickedPos();
        FluidState fluidstate = context.getLevel().getFluidState(context.getClickedPos());
        BlockPos blockpos1 = blockpos.north();
        BlockPos blockpos2 = blockpos.south();
        BlockPos blockpos3 = blockpos.west();
        BlockPos blockpos4 = blockpos.east();
        BlockState blockstate = iblockreader.getBlockState(blockpos1);
        BlockState blockstate1 = iblockreader.getBlockState(blockpos2);
        BlockState blockstate2 = iblockreader.getBlockState(blockpos3);
        BlockState blockstate3 = iblockreader.getBlockState(blockpos4);
        return this.defaultBlockState().setValue(NORTH, this.attachTo(blockstate,
                blockstate.isFaceSturdy(iblockreader, blockpos1, Direction.SOUTH))).setValue(SOUTH,
                this.attachTo(blockstate1, blockstate1.isFaceSturdy(iblockreader, blockpos2,
                        Direction.NORTH))).setValue(WEST, this.attachTo(blockstate2,
                blockstate2.isFaceSturdy(iblockreader, blockpos3, Direction.EAST))).setValue(EAST,
                this.attachTo(blockstate3, blockstate3.isFaceSturdy(iblockreader, blockpos4,
                        Direction.WEST))).setValue(WATERLOGGED, fluidstate.getType() == Fluids.WATER);
    }

    @Override
    public boolean attachTo(BlockState state, boolean flag) {
        Block block = state.getBlock();
        return !isExceptionForConnection(block) && flag || block instanceof Window || block.is(BlockTags.WALLS);
    }
}
